import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class DictionaryClientFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textFieldIta;
	private JTextField textFieldEng;
	private ButtonGroup bGroup;
	private JButton btnSend;
	private JRadioButton rdbtnTranslate, rdbtnPing, rdbtnAddWord;
	private JTextPane textPane;
	private Style redStyle, blackStyle, greenStyle;
	private JScrollPane scrollPane;

	// /**
	// * Launch the application.
	// */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// DictionaryClientFrame frame = new DictionaryClientFrame();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	public DictionaryClientFrame() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 347);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Information",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(6, 6, 438, 158);
		contentPane.add(panel);
		panel.setLayout(null);

		scrollPane = new JScrollPane();
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setBounds(6, 16, 426, 136);
		
		panel.add(scrollPane);

		textPane = new JTextPane();
		textPane.setEditable(false);
		scrollPane.setViewportView(textPane);
		textPane.setMargin(new Insets(4, 4, 4, 4));
		redStyle = textPane.addStyle("Red style", null);
		StyleConstants.setForeground(redStyle, Color.RED);
		blackStyle = textPane.addStyle("Black style", null);
		StyleConstants.setForeground(blackStyle, Color.BLACK);
		greenStyle = textPane.addStyle("Green style", null);
		StyleConstants.setForeground(greenStyle, Color.GREEN);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Option",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(6, 165, 140, 120);
		contentPane.add(panel_1);
		panel_1.setLayout(new GridLayout(3, 1, 0, 0));

		rdbtnTranslate = new JRadioButton("Translate");
		rdbtnTranslate.setSelected(true);
		panel_1.add(rdbtnTranslate);

		rdbtnAddWord = new JRadioButton("Add new word");
		panel_1.add(rdbtnAddWord);

		rdbtnPing = new JRadioButton("Ping");
		panel_1.add(rdbtnPing);

		bGroup = new ButtonGroup();
		bGroup.add(rdbtnPing);
		bGroup.add(rdbtnAddWord);
		bGroup.add(rdbtnTranslate);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Values",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(152, 165, 292, 120);
		contentPane.add(panel_2);
		panel_2.setLayout(null);

		JLabel lblItalian = new JLabel("Italian:");
		lblItalian.setBounds(19, 27, 53, 31);
		panel_2.add(lblItalian);

		textFieldIta = new JTextField();
		textFieldIta.setBounds(76, 27, 196, 31);
		panel_2.add(textFieldIta);
		textFieldIta.setColumns(10);

		JLabel lblNewLabel = new JLabel("English:");
		lblNewLabel.setBounds(19, 70, 53, 31);
		panel_2.add(lblNewLabel);

		textFieldEng = new JTextField();
		textFieldEng.setColumns(10);
		textFieldEng.setBounds(76, 71, 196, 31);
		panel_2.add(textFieldEng);

		btnSend = new JButton("Send Message");
		btnSend.setBounds(6, 290, 438, 29);
		contentPane.add(btnSend);
		getRootPane().setDefaultButton(btnSend);
	}

	public void setSendListener(ActionListener listener) {
		btnSend.addActionListener(listener);
	}

	public OperationType getMessageType() {
		if (rdbtnAddWord.isSelected()) {
			return OperationType.INSERT;
		} else if (rdbtnTranslate.isSelected()) {
			return OperationType.TRANSLATE;
		} else {
			return OperationType.PING;
		}
	}

	public void addText(String text) {
		textPane.setText(textPane.getText() + text + "\n");
		scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
	}

	public String getItalianWord() {
		return textFieldIta.getText();
	}

	public String getEnglishWord() {
		return textFieldEng.getText();
	}

	public void setErrorOnParameters() {
		StyledDocument doc = textPane.getStyledDocument();
		try {
			doc.insertString(doc.getLength(),
					"Errori nei parametri inseriti!\n", redStyle);
			scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
		} catch (BadLocationException e) {
		}
	}

	public void showMessage(Message m) {
		StyledDocument doc = textPane.getStyledDocument();
		try {
			doc.insertString(doc.getLength(), m.getDescription() + ": ",
					blackStyle);
			if (m.hasError()) {
				doc.insertString(doc.getLength(), "Request failed\n", redStyle);
				if (m.getOperation() == OperationType.INSERT)
					doc.insertString(
							doc.getLength(),
							"Word already exists. See the translation in the box below\n",
							redStyle);
			} else {
				doc.insertString(doc.getLength(), "Request succeed\n",
						greenStyle);
				if (m.getOperation() == OperationType.TRANSLATE)
					doc.insertString(
							doc.getLength(),
							"Word found. See the translation in the box below\n",
							greenStyle);
			}
		} catch (BadLocationException e) {
		}

		this.textFieldEng.setText(m.getEngWord());
		this.textFieldIta.setText(m.getItaWord());
		scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
	}

	public void setServerNotAvailable() {
		StyledDocument doc = textPane.getStyledDocument();
		try {
			doc.insertString(doc.getLength(), "Server non raggiungibile!\n",
					redStyle);
			scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum());
		} catch (BadLocationException e) {
		}
	}
}
